# Maintainer: Vi0L0 <vi0l093@gmail.com>
# Great Contributor: Shen Miren <dickeny@gmail.com> (build_module/hook idea and code)
# Great Contributor: Eduardo "kensai" Romero (previous catalyst and catalyst-utils maintainer)
# Contributor: Manuel Gaul <inkaine@hotmail.com> (previous lib32-catalyst-utils maintainer)
# Contributor: osmano807
# Contributor: csslayer
# Contributor: cyberpatrol
# Contributor: aidanlinz
# Contributor: Rip-Rip
# Contributor: OvsInc
# Contributor: Enverex & kidoz
# Contributor: nob
# Contributor: lordheavy
# Contributor: fnf
# Contributor: lano1106 (patch to fix support of intel's iommu and to remove dependency of kernel's CONFIG_AGP option)
# Contributor: lano1106 (patch to remove dependency of kernel's CONFIG_AGP option)
# Contributor: zoqaeski (authatieventsd' patch for lightdm)
# Contributor: lano1106 (patch to improve jacob's patch)
# Contributor: zoopp
# Contributor: solar (authatieventsd' patch s/-1/255)
# Contributor: Cold (current_euid patch)

# PKGEXT='.pkg.tar.gz' # imho time to pack this pkg into tar.xz is too long, unfortunatelly yaourt got problems when ext is different from .pkg.tar.xz - V

pkgname=catalyst-test
pkgver=14.9
_url="https://www2.ati.com/drivers/linux/amd-catalyst-14-9-linux-x86-x86-64.zip"
pkgrel=14
#_betano=1.0-jul11
_amdver="14.301.1001"
pkgdesc="AMD/ATI drivers, ${pkgver} AMD's version is ${_amdver}. catalyst-hook + catalyst-utils + lib32-catalyst-utils + experimental powerXpress suppport. Radeons HD 2 3 4 xxx ARE NOT SUPPORTED"
#pkgdesc="AMD/ATI drivers, ${pkgver} beta ${_betano}. catalyst-hook + catalyst-utils + lib32-catalyst-utils + experimental powerXpress suppport. Radeons HD 2 3 4 xxx ARE NOT SUPPORTED"
# pkgdesc="AMD/ATI testing/beta drivers, ${pkgver} beta ${_betano}. catalyst-hook + catalyst-utils + lib32-catalyst-utils + experimental powerXpress suppport. Radeons HD 2 3 4 xxx ARE NOT SUPPORTED"
#pkgdesc="AMD/ATI testing/beta drivers, ${_amdver} beta. catalyst-hook + catalyst-utils + lib32-catalyst-utils. Radeons HD 2 3 4 xxx ARE NOT SUPPORTED"
#pkgdesc="AMD/ATI testing/beta drivers, ${pkgver} beta. catalyst-hook + catalyst-utils + lib32-catalyst-utils. Radeons HD 2 3 4 xxx ARE NOT SUPPORTED"
# pkgdesc="AMD/ATI drivers. catalyst-hook + catalyst-utils + lib32-catalyst-utils. Radeons HD 2 3 4 xxx ARE NOT SUPPORTED"
arch=('i686' 'x86_64')
url="https://www.amd.com"
license=('custom')
options=('staticlibs' 'libtool' '!strip' '!upx')
depends=('linux>=3.0' 'linux<3.18' 'linux-headers' 'xorg-server>=1.7.0' 'xorg-server<1.17.0' 'libxrandr' 'libsm' 'fontconfig' 'libxcursor' 'libxi' 'gcc-libs' 'gcc>4.0.0' 'make' 'patch' 'libxinerama' 'mesa>=10.1.0-4')
optdepends=('qt4: to run ATi Catalyst Control Center amdcccle'
	    'libxxf86vm: to run ATi Catalyst Control Center amdcccle'
	    'opencl-headers: headers necessary for OpenCL development'
	    'acpid: acpi event support  / atieventsd'
	    'linux-lts-headers: to build the fglrx module for the linux-lts kernel')
conflicts=('libgl' 'catalyst' 'catalyst-daemon' 'catalyst-generator' 'catalyst-hook' 'catalyst-utils' 'libcl' 'catalyst-dkms' 'mesa-libgl' 'mesa-libgl-git')
provides=('libgl' "libatical=${pkgver}" "catalyst=${pkgver}" "catalyst-utils=${pkgver}" "catalyst-hook=${pkgver}" 'libcl' 'dri' 'libtxc_dxtn' 'mesa-libgl' 'mesa-libgl-git')

if [ "${CARCH}" = "x86_64" ]; then
 warning "x86_64 system detected"
 warning "[multilib] repository must be uncommented in /etc/pacman.conf to add lib32-catalyst-utils into the package"
  if [[ `cat /etc/pacman.conf | grep -c "#\[multilib]"` = 0 ]]; then
    warning "OK, lib32-catalyst-utils will be added to the package"
    depends+=('lib32-libxext' 'lib32-libdrm' 'lib32-libxinerama' 'lib32-mesa>=10.1.0-4')
    conflicts+=('lib32-libgl' 'lib32-catalyst-utils' 'lib32-libcl' 'lib32-mesa-libgl' 'lib32-mesa-libgl-git')
    provides+=('lib32-libgl' "lib32-catalyst-utils=${pkgver}" 'lib32-dri' 'lib32-libtxc_dxtn' 'lib32-libcl' 'lib32-mesa-libgl' 'lib32-mesa-libgl-git')
      else
	warning "lib32-catalyst-utils will NOT be added to the package"
  fi
fi

install=${pkgname}.install

url_ref="https://support.amd.com/en-us/kb-articles/Pages/latest-linux-beta-driver.aspx"
DLAGENTS="https::/usr/bin/curl --referer ${url_ref} -o %o %u"

source=(
#     https://www2.ati.com/drivers/beta/amd-driver-installer-catalyst-${pkgver/./-}-beta-x86.x86_64.zip
#     https://www2.ati.com/drivers/linux/amd-catalyst-${pkgver}-linux-x86.x86_64.zip
${_url}
#    https://www2.ati.com/drivers/beta/linux-amd-catalyst-${pkgver}-beta-v${_betano}.zip
    catalyst_build_module
    lib32-catalyst.sh
    catalyst.sh
    atieventsd.sh
    atieventsd.service
    catalyst.conf
    arch-fglrx-authatieventsd_new.patch
    hook-fglrx
    ati_make.sh
    makefile_compat.patch
    catalyst-hook.service
    switchlibGL
    switchlibglx
    pxp_switch_catalyst
    temp_links_catalyst
    temp-links-catalyst.service
    lano1106_fglrx_intel_iommu.patch
    lano1106_kcl_agp_13_4.patch
    lano1106_fglrx-13.8_proc.patch
    cold-fglrx-3.14-current_euid.patch
    fglrx_gpl_symbol.patch
    no_hotplug_fix.patch
    fglrx_3.17rc6-no_hotplug_half.patch
    )


md5sums=('117f757f941c885ec1b771517551a602'
         'ddd9ca6ec020e4f7bfe9cf5063b1773c'
         'af7fb8ee4fc96fd54c5b483e33dc71c4'
         'bdafe749e046bfddee2d1c5e90eabd83'
         '9d9ea496eadf7e883d56723d65e96edf'
         '90a37e010f4e5f45e270cd000894d553'
         '3e19c2285c76f4cb92108435a1e9c302'
         'b3ceefeb97c609037845f65d0956c4f0'
         '9126e1ef0c724f8b57d3ac0fe77efe2f'
         '62239156a9656c6f41e89a879578925c'
         '3e1b82bd69774ea808da69c983d6a43b'
         'a64e2eae5addc6d670911ccf94b8cda4'
         '394bc493fdf493a5093f9e2095096d02'
         '3226230592fa3c91ff22389114fc5dc7'
         '9e706c272feb167be55ba7201dfa8d51'
         '0e6d963436dd23dbb45ae0f4fc9b661c'
         '2c22bb4d4f828cb8b024f670c1ae7e45'
         '5184b94a2a40216a67996999481dd9ee'
         'c5156eddf81c8a1719b160d05a2e8d67'
         '2ab4837233de42332753882445373d7b'
         'ba33b6ef10896d3e1b5e4cd96390b771'
         'ef97fc080ce7e5a275fe0c372bc2a418'
         '8f5198d944f9114238ff080aba74f178'
         '244cbfce7c0ca790cf7735cc962b1f8b')
sha512sums=('022c4b6660c586e6055c2a4f708ca8f37677dc99a32b00fb8fe0ef51981c872a3cae0014994f2b9d137df0b8e98b6ab9904af778d77a815b62a5d72404022bb9'
            '9ca2a4dc72df632093d7d73e4b61186873c90be74f0556e0cb622546c968a891207328e0d21d18a545f882f729f88917e62dde3fa3c15e4b75279b2baae819ff'
            '2b0e81b7019ef8ceaef66901128b54b684be0c65315c99d1b64c7c9bfc8569f9b3ea165ce6cfb77fdb4b280566650a1e0b3fe6c9ea67643a6872dfc3ac321ae1'
            '06294ab23e4f1610a27e1670df343dd6ab91c93455633f720fb97efff65fbd736286ae5a459cdca4ab422d6c9b3f70df4b4df018c97b55590eff41352464c934'
            '65d2a192782105b311e67849f8b96445b9bb632a0c41f164a1c2e87dec9d27f1ac495b4542b4a1e9de78faff97a26c9c22b3d60efb9bf0c8a88b979383190c04'
            '06b833b1accf4543568d01989e1d8575168a374e62bdb170702187085d952c5a46cddb1d85068c86742f246b013bd9f66e5c6acdf697cdc157b4ffef6c1b531b'
            'e3edc24897ac3e1d65c856f13f403c17ca914d368d64194278a6a04b6e18997e1bfb3bade22db8d5df76c7dfc6410ff0264dd268dd3394d7fbf2301b349367ee'
            '7d6d8dbb54cafc138176230dd8e1459a0b2464d7f8a20df345e697d78d31835bb09fb2f1d65ddc053b4d90bbfd496579a38ee9ec839cf41ece790b91be88487b'
            'c76f265f837025116d8380efaea69cd1b51e780fe680c6e7db9243292daddec93f5696f925cc4c65b6c969fdcf7269768a028e4e2d1f297ce931be7b047f62b7'
            '491ad85f376559fec332c3f7fb9be4bb7f2fd2cb1c520b30f98a12e306dbac7f81ea112736260ca029a2be98a051626ababefae9159de9232e3a61d130910c3b'
            'd3a6eb9010f35f010ef1c65bc7b3b85527ea3607af15d44d5f6380d21c36b817abdd3152b6eb9f6c0e8647e772111eebe1de271c999a69a220d5f43ad741ac5d'
            'f84d96f3f41db6076948e66816d0fffc07feeab8c1715a36fe9d0626823a4f56987a0826580805464cda8f59080597f92681af309a43678481042aed56135256'
            'e179927fdf24463a42737c313a4fd691e581ddc0e566ff539f0920a2d95aa435259568eb3adbb0b8f55456402ffc9f9fb8d257addbc6deadc5d11f7f46cbd013'
            '819d54ff3665a578085e0f79a27d7aa42c9d4e81007528896ffc15a70e9a4ec34bd8ca805d596bd0de490b01f52550bbc9146f4da6e99e8b55c4e3ea9f89a418'
            '9dab02917d63123301bc1dddd6c5ac31cace47f7660ce5f5ae08006516ceafb225522f04238d72358b5580ebfdc86c268f1df9d07cec1c6ff20abc94356fe1ce'
            'fd6dac76c24972cffe2f2876de29bd89e9602009ab5e36277d88cec49d23a41a2f5875594dbeaf2e684e44c796e7d351febecad7336d84ec92f31e2d58ab2049'
            '240732aa8011a8fa177001bdba8999d5fba58a977bb5f8b9787e17d07ed7bcadcefc8d0fcee40c2a558aa8170feccd32ad82f77c0981b4256f2cb5e720f28488'
            'f9ded14b8861d51849b0df1dbb7ed60bc9e7685d9d746a0492c351d20ebc9c7565f3fac884c3b26aa39d4255bcbadba0182efd2032c3ed263cc1d06f60b847a6'
            'f8e7bb164567cfd27dafddd79fc750e06792753fcd990b093133861457c1a45adbc7b513d55f2ae1a8df83eb98a854349806ea8662359947987d904dea805bd5'
            'ad6e2089368387225f730e2f9bf23bd1a87fb5b799d7d8945a541c7013f38d52b42f2303e858a8c294797e1e8c513d09c1bcae6d95a63726ac6ebf118c5b5c35'
            '904ec2be57eaa6f51e159e95830c3eae5d12845a3321fb12d0d8c21a701aacea98037b45997bda2f3ab3e26bbc3c61251f337edc9bd7d21e71f29cb15f42689d'
            '263c4cdfadc4e77ee25be6a64a495fa626e3c3e1ea0330d2b126bfb5900a2b5ca1ea8fece15a342a1024b9c848611ff72e2f3dbd9efe75b8c7fd4d754efdfa36'
            '9da46cf9dcf6919777bba7cbf00ecc539d94814681d31eb6850cba8052aedcfdc869efdd1bb8608b96da63abc1298cabe04c38f72e7eb0d078c628fee7b16d8b'
            'a392a3fb98ac3f5f39a3a0d75a30f7bde9c134dd1f6d6ae7f0c92657f831c8eef1690190e30e88ccf468ddacc1fd5e679259873b308dc0f54506c7b420ae5278')



build() {
  ## Unpack archive
#      /bin/sh ./amd-catalyst-${pkgver}-linux-x86.x86_64.run --extract archive_files
     /bin/sh ./fglrx-${_amdver}/amd-driver-installer-${_amdver}-x86.x86_64.run --extract archive_files
#mkdir common
#mv etc lib usr common
#mkdir archive_files
#mv arch common xpic xpic_64a archive_files
}

package() {
##catalyst-utils section
  ## Install userspace tools and libraries
    # Create directories
      install -m755 -d ${pkgdir}/etc/ati
      install -m755 -d ${pkgdir}/etc/rc.d
      install -m755 -d ${pkgdir}/etc/profile.d
      install -m755 -d ${pkgdir}/etc/acpi/events
      install -m755 -d ${pkgdir}/etc/security/console.apps
      install -m755 -d ${pkgdir}/etc/OpenCL/vendors

      install -m755 -d ${pkgdir}/usr/lib/xorg/modules/dri
      install -m755 -d ${pkgdir}/usr/lib/xorg/modules/drivers
      install -m755 -d ${pkgdir}/usr/lib/xorg/modules/extensions
      install -m755 -d ${pkgdir}/usr/lib/xorg/modules/extensions/fglrx
      install -m755 -d ${pkgdir}/usr/lib/xorg/modules/linux
      install -m755 -d ${pkgdir}/usr/lib/dri
      install -m755 -d ${pkgdir}/usr/lib/fglrx
      install -m755 -d ${pkgdir}/usr/lib/systemd/system
#       install -m755 -d ${pkgdir}/usr/lib/hsa		#removed in 14.1

      install -m755 -d ${pkgdir}/usr/bin

      install -m755 -d ${pkgdir}/usr/include/GL

      install -m755 -d ${pkgdir}/usr/share/applications
      install -m755 -d ${pkgdir}/usr/share/ati/amdcccle
      install -m755 -d ${pkgdir}/usr/share/licenses/${pkgname}
      install -m755 -d ${pkgdir}/usr/share/man/man8
      install -m755 -d ${pkgdir}/usr/share/pixmaps

    # X.org driver
      if [ "${CARCH}" = "i686" ]; then
	cd ${srcdir}/archive_files/xpic/usr/X11R6/lib/modules
      elif [ "${CARCH}" = "x86_64" ]; then
	cd ${srcdir}/archive_files/xpic_64a/usr/X11R6/lib64/modules
      fi

      install -m755 *.so ${pkgdir}/usr/lib/xorg/modules
      install -m755 drivers/*.so ${pkgdir}/usr/lib/xorg/modules/drivers
      install -m755 linux/*.so ${pkgdir}/usr/lib/xorg/modules/linux
      install -m755 extensions/fglrx/fglrx-libglx.so ${pkgdir}/usr/lib/xorg/modules/extensions/fglrx/fglrx-libglx.so
      ln -snf /usr/lib/xorg/modules/extensions/fglrx/fglrx-libglx.so ${pkgdir}/usr/lib/xorg/modules/extensions/libglx.so

    # Controlcenter / libraries
      if [ "${CARCH}" = "i686" ]; then
	cd ${srcdir}/archive_files/arch/x86/usr
	_lib=lib
      elif [ "${CARCH}" = "x86_64" ]; then
	cd ${srcdir}/archive_files/arch/x86_64/usr
	_lib=lib64
      fi

      install -m755 X11R6/bin/* ${pkgdir}/usr/bin
      install -m755 sbin/* ${pkgdir}/usr/bin
      install -m755 X11R6/${_lib}/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib/fglrx
      install -m755 X11R6/${_lib}/libAMDXvBA.so.1.0 ${pkgdir}/usr/lib
      ln -snf libAMDXvBA.so.1.0 ${pkgdir}/usr/lib/libAMDXvBA.so.1
      ln -snf libAMDXvBA.so.1.0 ${pkgdir}/usr/lib/libAMDXvBA.so
      install -m755 X11R6/${_lib}/libatiadlxx.so ${pkgdir}/usr/lib
      install -m755 X11R6/${_lib}/libfglrx_dm.so.1.0 ${pkgdir}/usr/lib
      install -m755 X11R6/${_lib}/libXvBAW.so.1.0 ${pkgdir}/usr/lib
      ln -snf libXvBAW.so.1.0 ${pkgdir}/usr/lib/libXvBAW.so.1
      ln -snf libXvBAW.so.1.0 ${pkgdir}/usr/lib/libXvBAW.so
      install -m644 X11R6/${_lib}/*.a ${pkgdir}/usr/lib
      install -m644 X11R6/${_lib}/*.cap ${pkgdir}/usr/lib
      install -m755 X11R6/${_lib}/modules/dri/*.so ${pkgdir}/usr/lib/xorg/modules/dri
      install -m755 ${_lib}/*.so* ${pkgdir}/usr/lib
#       install -m755 ${_lib}/hsa/* ${pkgdir}/usr/lib/hsa		#removed in 14.1

    ## QT libs (only 2 files) - un-comment 2 lines below if you don't want to install qt package
#      install -m755 -d ${pkgdir}/usr/share/ati/${_lib}
#      install -m755 share/ati/${_lib}/*.so* ${pkgdir}/usr/share/ati/${_lib}

      ln -snf /usr/lib/xorg/modules/dri/fglrx_dri.so ${pkgdir}/usr/lib/dri/fglrx_dri.so
      ln -snf libfglrx_dm.so.1.0 ${pkgdir}/usr/lib/libfglrx_dm.so.1
      ln -snf libfglrx_dm.so.1.0 ${pkgdir}/usr/lib/libfglrx_dm.so
      ln -snf libatiuki.so.1.0 ${pkgdir}/usr/lib/libatiuki.so.1
      ln -snf libatiuki.so.1.0 ${pkgdir}/usr/lib/libatiuki.so

      ln -snf libOpenCL.so.1 ${pkgdir}/usr/lib/libOpenCL.so

      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib/fglrx/libGL.so.1.2.0
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib/fglrx/libGL.so.1
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib/fglrx/libGL.so
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib/libGL.so.1.2.0
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib/libGL.so.1
      ln -snf /usr/lib/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib/libGL.so


      # We have to provide symlinks to mesa, as catalyst doesn't ship them
      ln -s /usr/lib/mesa/libEGL.so.1.0.0 ${pkgdir}/usr/lib/libEGL.so.1.0.0
      ln -s libEGL.so.1.0.0	          ${pkgdir}/usr/lib/libEGL.so.1
      ln -s libEGL.so.1.0.0               ${pkgdir}/usr/lib/libEGL.so

      ln -s /usr/lib/mesa/libGLESv1_CM.so.1.1.0 ${pkgdir}/usr/lib/libGLESv1_CM.so.1.1.0
      ln -s libGLESv1_CM.so.1.1.0	        ${pkgdir}/usr/lib/libGLESv1_CM.so.1
      ln -s libGLESv1_CM.so.1.1.0               ${pkgdir}/usr/lib/libGLESv1_CM.so

      ln -s /usr/lib/mesa/libGLESv2.so.2.0.0 ${pkgdir}/usr/lib/libGLESv2.so.2.0.0
      ln -s libGLESv2.so.2.0.0               ${pkgdir}/usr/lib/libGLESv2.so.2
      ln -s libGLESv2.so.2.0.0               ${pkgdir}/usr/lib/libGLESv2.so


      cd ${srcdir}/archive_files/common
      patch -Np2 -i ${srcdir}/arch-fglrx-authatieventsd_new.patch
      install -m644 etc/ati/* ${pkgdir}/etc/ati
      chmod 755 ${pkgdir}/etc/ati/authatieventsd.sh

      install -m644 etc/security/console.apps/amdcccle-su ${pkgdir}/etc/security/console.apps

      install -m755 usr/X11R6/bin/* ${pkgdir}/usr/bin
      install -m644 usr/include/GL/*.h ${pkgdir}/usr/include/GL
      install -m755 usr/sbin/*.sh ${pkgdir}/usr/bin
      install -m644 usr/share/ati/amdcccle/* ${pkgdir}/usr/share/ati/amdcccle
      install -m644 usr/share/icons/*.xpm ${pkgdir}/usr/share/pixmaps
      install -m644 usr/share/man/man8/*.8 ${pkgdir}/usr/share/man/man8
      install -m644 usr/share/applications/*.desktop ${pkgdir}/usr/share/applications

    # ACPI example files
      install -m755 usr/share/doc/fglrx/examples/etc/acpi/*.sh ${pkgdir}/etc/acpi
      sed -i -e "s/usr\/X11R6/usr/g" ${pkgdir}/etc/acpi/ati-powermode.sh
      install -m644 usr/share/doc/fglrx/examples/etc/acpi/events/* ${pkgdir}/etc/acpi/events

    # Add ATI Events Daemon launcher
      install -m755 ${srcdir}/atieventsd.sh ${pkgdir}/etc/rc.d/atieventsd
      install -m644 ${srcdir}/atieventsd.service ${pkgdir}/usr/lib/systemd/system

    # thanks to cerebral, we dont need that damned symlink
      install -m755 ${srcdir}/catalyst.sh ${pkgdir}/etc/profile.d

    # License
      install -m644 ${srcdir}/archive_files/LICENSE.TXT ${pkgdir}/usr/share/licenses/${pkgname}
      install -m644 ${srcdir}/archive_files/common/usr/share/doc/amdcccle/ccc_copyrights.txt \
	${pkgdir}/usr/share/licenses/${pkgname}/amdcccle_copyrights.txt

    # since 11.11 : opencl files
      if [ "${CARCH}" = "i686" ]; then
	cd ${srcdir}/archive_files/arch/x86
	_arc=32
      elif [ "${CARCH}" = "x86_64" ]; then
	cd ${srcdir}/archive_files/arch/x86_64
	_arc=64
      fi

      install -m644 etc/OpenCL/vendors/amdocl${_arc}.icd ${pkgdir}/etc/OpenCL/vendors
      install -m755 usr/bin/clinfo ${pkgdir}/usr/bin
      install -m755 -d ${pkgdir}/etc/modules-load.d
      install -m644 ${srcdir}/catalyst.conf ${pkgdir}/etc/modules-load.d

    #workaround for the high io bug , thanks to lano1106 for finding this ugly bug! https://bbs.archlinux.org/viewtopic.php?pid=1279977#p1279977
      install -m755 ${srcdir}/temp_links_catalyst ${pkgdir}/usr/bin
      install -m644 ${srcdir}/temp-links-catalyst.service ${pkgdir}/usr/lib/systemd/system

     # powerXpress
      install -m755 ${srcdir}/switchlibGL ${pkgdir}/usr/lib/fglrx
      install -m755 ${srcdir}/switchlibglx ${pkgdir}/usr/lib/fglrx
      # switching script: switch xorg.conf + aticonfig --px-Xgpu + switchlibGL + add/remove fglrx into MODULES
      install -m755 ${srcdir}/pxp_switch_catalyst ${pkgdir}/usr/bin

##catalyst-hook section
      cd ${srcdir}/archive_files
      patch -Np1 -i ../makefile_compat.patch
      patch -Np1 -i ../lano1106_fglrx_intel_iommu.patch
      patch -Np1 -i ../lano1106_kcl_agp_13_4.patch
      patch -Np1 -i ../lano1106_fglrx-13.8_proc.patch
      patch -Np1 -i ../cold-fglrx-3.14-current_euid.patch
      test "${CARCH}" = "i686" && patch -Np1 -i ../fglrx_gpl_symbol.patch
      patch -Np1 -i ../no_hotplug_fix.patch
      patch -Np1 -i ../fglrx_3.17rc6-no_hotplug_half.patch

    # Prepare modules source files
      _archdir=x86_64
      test "${CARCH}" = "i686" && _archdir=x86
      install -m755 -d ${pkgdir}/usr/share/ati/build_mod
      install -m644 common/lib/modules/fglrx/build_mod/*.c \
                ${pkgdir}/usr/share/ati/build_mod
      install -m644 common/lib/modules/fglrx/build_mod/*.h \
                ${pkgdir}/usr/share/ati/build_mod
      install -m644 common/lib/modules/fglrx/build_mod/2.6.x/Makefile \
                ${pkgdir}/usr/share/ati/build_mod
      install -m644 arch/${_archdir}/lib/modules/fglrx/build_mod/libfglrx_ip.a \
                ${pkgdir}/usr/share/ati/build_mod
      install -m755 -d ${pkgdir}/usr/bin
      install -m755 ${srcdir}/catalyst_build_module ${pkgdir}/usr/bin

    # modified ati's make.sh script
      install -m755 ${srcdir}/ati_make.sh ${pkgdir}/usr/share/ati/build_mod

    # hook fglrx
      install -m755 -d ${pkgdir}/usr/lib/initcpio/install
      install -m644 ${srcdir}/hook-fglrx ${pkgdir}/usr/lib/initcpio/install/fglrx

    # systemd service to perform fglrx module build at shutdown
      install -m755 -d ${pkgdir}/usr/lib/systemd/system
      install -m644 ${srcdir}/catalyst-hook.service ${pkgdir}/usr/lib/systemd/system


##lib32-catalyst-utils section
      if [ "${CARCH}" = "x86_64" ] && [[ `cat /etc/pacman.conf | grep -c "#\[multilib]"` = 0 ]]; then
	cd ${srcdir}
	install -D -m755 lib32-catalyst.sh ${pkgdir}/etc/profile.d/lib32-catalyst.sh
	cd ${srcdir}/archive_files/arch/x86/usr
	install -dm755 ${pkgdir}/usr/lib32
	install -dm755 ${pkgdir}/usr/lib32/fglrx
	install -dm755 ${pkgdir}/usr/lib32/xorg/modules/dri
#	install -dm755 ${pkgdir}/usr/lib32/hsa		#removed in 14.1
	install -m755 lib/*.so* ${pkgdir}/usr/lib32
#	install -m755 lib/hsa/* ${pkgdir}/usr/lib32/hsa		#removed in 14.1
	install -m755 X11R6/lib/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib32/fglrx
	install -m755 X11R6/lib/libAMDXvBA.so.1.0 ${pkgdir}/usr/lib32
	install -m755 X11R6/lib/libatiadlxx.so ${pkgdir}/usr/lib32
	install -m755 X11R6/lib/libfglrx_dm.so.1.0 ${pkgdir}/usr/lib32
	install -m755 X11R6/lib/libXvBAW.so.1.0 ${pkgdir}/usr/lib32
	install -m755 X11R6/lib/modules/dri/*.so ${pkgdir}/usr/lib32/xorg/modules/dri
	ln -snf /usr/lib32/xorg/modules/dri ${pkgdir}/usr/lib32/dri

	cd $pkgdir/usr/lib32
	ln -sf /usr/lib32/libfglrx_dm.so.1.0	${pkgdir}/usr/lib32/libfglrx_dm.so.1
	ln -sf /usr/lib32/libfglrx_dm.so.1.0	${pkgdir}/usr/lib32/libfglrx_dm.so
	ln -sf /usr/lib32/libAMDXvBA.so.1.0	${pkgdir}/usr/lib32/libAMDXvBA.so.1
	ln -sf /usr/lib32/libAMDXvBA.so.1.0	${pkgdir}/usr/lib32/libAMDXvBA.so
	ln -sf /usr/lib32/libXvBAW.so.1.0	${pkgdir}/usr/lib32/libXvBAW.so.1
	ln -sf /usr/lib32/libXvBAW.so.1.0	${pkgdir}/usr/lib32/libXvBAW.so
	ln -sf /usr/lib32/libatiuki.so.1.0	${pkgdir}/usr/lib32/libatiuki.so.1
	ln -sf /usr/lib32/libatiuki.so.1.0	${pkgdir}/usr/lib32/libatiuki.so

	ln -sf /usr/lib32/libOpenCL.so.1	${pkgdir}/usr/lib32/libOpenCL.so

	ln -sf /usr/lib32/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib32/fglrx/libGL.so.1.2.0
	ln -sf /usr/lib32/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib32/fglrx/libGL.so.1
	ln -sf /usr/lib32/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib32/fglrx/libGL.so
	ln -sf /usr/lib32/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib32/libGL.so.1.2.0
	ln -sf /usr/lib32/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib32/libGL.so.1
	ln -sf /usr/lib32/fglrx/fglrx-libGL.so.1.2 ${pkgdir}/usr/lib32/libGL.so


      # We have to provide symlinks to lib32-mesa, as catalyst doesn't ship them
	ln -s /usr/lib32/mesa/libEGL.so.1.0.0 ${pkgdir}/usr/lib32/libEGL.so.1.0.0
	ln -s libEGL.so.1.0.0	              ${pkgdir}/usr/lib32/libEGL.so.1
	ln -s libEGL.so.1.0.0                 ${pkgdir}/usr/lib32/libEGL.so

	ln -s /usr/lib32/mesa/libGLESv1_CM.so.1.1.0 ${pkgdir}/usr/lib32/libGLESv1_CM.so.1.1.0
	ln -s libGLESv1_CM.so.1.1.0	            ${pkgdir}/usr/lib32/libGLESv1_CM.so.1
	ln -s libGLESv1_CM.so.1.1.0                 ${pkgdir}/usr/lib32/libGLESv1_CM.so

	ln -s /usr/lib32/mesa/libGLESv2.so.2.0.0 ${pkgdir}/usr/lib32/libGLESv2.so.2.0.0
	ln -s libGLESv2.so.2.0.0                 ${pkgdir}/usr/lib32/libGLESv2.so.2
	ln -s libGLESv2.so.2.0.0                 ${pkgdir}/usr/lib32/libGLESv2.so


	# OpenCL
	install -m755 -d ${pkgdir}/etc/OpenCL/vendors
	install -m644 ${srcdir}/archive_files/arch/x86/etc/OpenCL/vendors/amdocl32.icd ${pkgdir}/etc/OpenCL/vendors
      fi
}
